<?php
/************************************
 摘自:http://www.dwww.cn/new/2006291929111039.html 
 名称: php分页类
 修改作者:周东平
 修改日期: 2006-04-08 
 修改函数:function getPageString($flag=1){}
           使其更方便,灵活,代码精简
***********************************/
 class Pager{ 
   //生成的页码（事实上不用也行） 
   var $pageNumber = '';    
   //需要分类的条目总数 
   var $totalItems = 0; 
   var $sql; 
   //每页显示几个条目 
   var $itemsPerPage=3;    
   //总页数 
   var $totalPageNumber=0; 
   //当前页码！ 
   var   $currentPageNumber=1;     
   //需要分页的url 
   var $param = '';
   var $url ='';
   function Pager($sql,$currentPageNumber,$itemsPerPage,$param){
     if (! $currentPageNumber) 
		$currentPageNumber = 1;
     $this -> currentPageNumber = $currentPageNumber; 
     $this -> sql = $sql;
     $this -> itemsPerPage = $itemsPerPage;
     $this -> param = $param; 
     $this -> url.=(stristr($this->url,'?')!=false) ? '&' : '?';  //Url里有"?"就加"&"没有就加"?" 
     $this -> getTotalPageNumber();
   }
   //返回记录的总数
    function getTotalItems(){
     //for adodb
     $rs = mysql_query($this -> sql);
     $this -> totalItems = mysql_num_rows($rs);
     return $this -> totalItems;
    }
   //返回总页数
   function getTotalPageNumber(){ 
      $this -> totalPageNumber = ceil($this -> getTotalItems() / $this -> itemsPerPage);   
      return $this -> totalPageNumber;
   } 
   //返回当前页
   function getCurrentPage(){ 
     if ($this -> $currentPageNumber == 0)
  	    $this -> $currentPageNumber = 1;
      return $this -> $currentPageNumber;
   } 
   //SQL里 LIMIT start，length 中的起始值 
   function getLimitStart(){ 
      $start = ($this -> currentPageNumber-1) * $this -> itemsPerPage; 
	  if ($start < 0)
	   $start = 0;
      return $start; 
   } 
   //SQL里 LIMIT start，length 中的length 
   function getLimitItems(){ 
      return $this -> itemsPerPage; 
   } 
   //返回数据
   function getRsPerPage(){
     $start = $this -> getLimitStart();
	 $num = $this -> getLimitItems();
	 $sql = $this -> sql . " limit $start,$num";
	 $modiRS = mysql_query($sql);      
     return $modiRS;
   }

   //主函数.分页函数
   function getPageString($flag=1){ 
     if ($this -> getTotalPageNumber() > 0)
     { 
	  if ( $flag == "en_")  //英语
	  {
		 $first_str = "FIRST";
		 $prev_str = "PREV";
		 $next_str = "NEXT";
		 $last_str = "LAST";
		 $to_str = "Goto Page";
		 $colon = ":";		 
		 $page_str = "Pages ";
		 $page_s = "Page";
		 $curr_page = "Position:";
		 $page_total = "Total:";
		 $record_str = "Total:";
		 $di = "";		 
	  } else {        //中文
		 $first_str = "首页";
		 $prev_str = "上页";
		 $next_str = "下页";
		 $last_str = "尾页";
		 $to_str = " 到";
		 $colon = "：";
		 $curr_page = "当前位置" . $colon;		 
		 $page_str = "页";
		 $page_s = "页";		 
		 $page_total = "共有";
		 $record_str = "总数" . $colon;
		 $di = "第";
	  } //end if  
      $page_string = '';
	  $page = $this -> currentPageNumber;  //当前显示的页数
	  $page_count = $this -> totalPageNumber;  //页数总数
	  $getval = "&" . $this -> param;
      if($page == 1 ){
        $page_string .= "[". $first_str.'] ['.$prev_str ."] ";
      } else {
        $page_string .= "[<a href=?p=1" . $getval . ">" . $first_str . "</a>] [<a href=?p=".($page-1).$getval.">" .$prev_str ."</a>] ";
      } //end if
      if( ($page == $page_count) || ($page_count == 0) ){
        $page_string .= "[". $next_str.'] ['. $last_str."]";
      } else {
         $page_string .= "[<a href=?p=".($page+1).$getval.">" .$next_str . "</a>] [<a href=?p=".$page_count.$getval.">" .$last_str . "</a>] ";
      } //end if
      $tem = "'?p='+this.value+'{$getval}'";
      $str = "javascript:window.open($tem,target='_self');";
      $page_string .= "    &nbsp;&nbsp;&nbsp;&nbsp;" . $curr_page . " " . $di . " " . $page . " " . $page_s . " " . $page_total . " " . $page_count . " " . $page_str;
      $page_string.=" " . $record_str . $this -> totalItems;
	  $page_string .= "";
      return $page_string;          
    }             
   } 
}
?> 
